package com.data.modules.wrt.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.data.common.aspect.annotation.AutoLog;
import com.data.common.util.JiguangPushUtils;
import com.data.common.util.WrtPushUtils;
import com.data.modules.wrt.entity.WrtRoom;
import com.data.modules.wrt.mapper.WrtRoomMapper;
import com.data.modules.wrt.service.IWrtRoomService;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;

/**
 * @Description: 慧锐通房间信息
 * @Author: data-boot
 * @Date:   2023-09-08
 * @Version: V1.0
 */
@Slf4j
@Service
public class WrtRoomServiceImpl extends ServiceImpl<WrtRoomMapper, WrtRoom> implements IWrtRoomService {

    @Autowired
    private WrtRoomMapper wrtRoomMapper;

    @Override
    public void saveRoomInfo(List<WrtRoom> list) {

        list.stream().forEach(x->{

            LambdaQueryWrapper<WrtRoom> lambdaQueryWrapper = new QueryWrapper<WrtRoom>()
                    .lambda();
           // lambdaQueryWrapper.eq(WrtRoom::getUuid,x.getUuid());
            lambdaQueryWrapper.eq(WrtRoom::getSn,x.getSn());
           // lambdaQueryWrapper.eq(WrtRoom::getName,x.getName());

            List<WrtRoom> users = baseMapper.selectList(lambdaQueryWrapper);
            if(ObjectUtil.isEmpty(users)){
                this.wrtRoomMapper.insert(x);
            }

        });
    }
    @Override
    public void synRoomInfo(String parameter) {

        String pid = "217";
        String url = "https://client.wrtrd.com/wrtservice/resource/801api?pid="+pid+"&method=get_room";
        //登录wrt 验证
        String result = WrtPushUtils.wrtPush();
        JSONObject resData = JSONObject.fromObject(result);
        if(resData.containsKey("access_token")){
            String token = (String) resData.get("access_token");
            log.info("返回结果"+resData);
            String resultRoom = WrtPushUtils.sendWrtPostRequest(url,"","UTF-8",token);

            if(ObjectUtil.isNotEmpty(resultRoom)){
                JSONObject room = JSONObject.fromObject(resultRoom);
                int code = room.getInt("code");
                if(code == 1){
                    JSONObject data= room.getJSONObject("data");
                    JSONArray rooms = data.getJSONArray("rooms");
                    List<WrtRoom> entityList = com.alibaba.fastjson.JSONArray.parseArray(rooms.toString(), WrtRoom.class);

                    entityList.stream().forEach(x->{

                        x.setUuid(pid);
                        LambdaQueryWrapper<WrtRoom> lambdaQueryWrapper = new QueryWrapper<WrtRoom>()
                                .lambda();
                        lambdaQueryWrapper.eq(WrtRoom::getUuid,x.getUuid());
                        lambdaQueryWrapper.eq(WrtRoom::getSn,x.getSn());
                        lambdaQueryWrapper.eq(WrtRoom::getName,x.getName());

                        List<WrtRoom> users = baseMapper.selectList(lambdaQueryWrapper);
                        if(ObjectUtil.isEmpty(users)){
                            this.wrtRoomMapper.insert(x);
                        }

                    });
                }
            }

        }

    }
}
